<?php

class Form {

//<editor-fold defaultstate="collapsed" desc="Champs">
    private $_id = "inconnu";
    private $_action = "";
    private $_method = "POST";

    private $_titre = "";

    private $_textHTML = "";
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="Propriétés">
    public function __get($propriete){
        switch ($propriete){
            case "id": return $this->_id;
                break;
            case "action": return $this->_action;
                break;
            case "method": return $this->_method;
                break;
            case "titre": return $this->_titre;
                break;
            case "textHTML": return $this->_textHTML;
                break;
            default: throw new Exception("Propriété '$propriete' introuvable");
        }
    }

    public function __set($propriete, $value){
        switch ($propriete){
            case "id": $this->_id = $value;
                break;
            case "action": $this->_action = $value;
                break;
            case "method": $this->_method = $value;
                break;
            case "titre": $this->_titre = $value;
                break;
            case "textHTML": $this->_textHTML = $value;
                break;
            default: throw new Exception("Propriété '$propriete' introuvable");
        }
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="Constructeur">
    public function __construct($id, $action, $method, $titre = null){
        $this->id = $id;
        $this->action = $action;
        $this->method = $method;

        $divTitre = ($titre==null)?"":"<div class='titre'>$titre</div>";

        $this->textHTML .= "<form id='$id' action='$action' method='$method'><br />$divTitre<br />";
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="Methodes">
    public function beginFieldset($class = null, $id = null){
        $this->textHTML .= "<fieldset";
        if ($class != null){
            $this->textHTML .= " class='$class'";
        }
        if ($id != null){
            $this->textHTML .= " id='$id'";
        }
        $this->textHTML .= ">";
    }

    public function endFieldset(){
        $this->textHTML .= "</fieldset>";
    }

    public function addLegend($titre){
        $this->textHTML .= "<legend>$titre</legend>";
    }

    /**
     * @param $type InputType
     * @param $name
     * @param null $id
     * @param null $valueInput
     * @param null $checked
     * @param null $class
     * @param null $option
     * @param null $src
     */
    public function addInput($type, $name, $id = null, $valueInput = null, $checked = null, $class = null, $option = null, $src = null){
        $this->textHTML .= "<input type='$type' name='$name'";
        if ($id != null){
            $this->textHTML .= " id='$id'";
        }
        if ($class != null) {
            $this->textHTML .= " class='$class'";
        }
        if ($valueInput != null){
            $this->textHTML .= " value='$valueInput'";
        }
        if ($checked != null){
            $this->textHTML .= " checked='$checked'";
        }
        if ($option != null) {
            $this->textHTML .= $option;
        }
        if ($src != null){
            $this->textHTML .= " src='$src'";
        }
        $this->textHTML .= "/>";
    }

    public function addSpan($text = null , $id = null, $class = null){
        $this->textHTML .= "<span";
        if ($id != null){
            $this->textHTML .= " id='$id'";
        }
        if ($class != null){
            $this->textHTML .= " class='$class'";
        }
        $this->textHTML .= ">";
        if ($text != null){
            $this->textHTML .= $text;
        }
        $this->textHTML .= "</span>";
    }

    public function addLabel($for = null, $class = null, $id = null, $text = null){
        $this->textHTML .= "<label";
        if ($for != null){
            $this->textHTML .= " for='$for'";
        }
        if ($class != null){
            $this->textHTML .= " class='$class'";
        }
        if ($id != null){
            $this->textHTML .= " id='$id'";
        }
        $this->textHTML .= ">";
        if ($text !=null){
            $this->textHTML .= $text;
        }
        $this->textHTML .= "</label>";
    }

    public function addHref($href, $class = null, $id = null, $text = null){
        $this->textHTML .= "<a href='$href'";
        if ($class != null){
            $this->textHTML .= " class='$class'";
        }
        if ($id != null){
            $this->textHTML .= " id='$id'";
        }
        $this->textHTML .= ">";
        if ($text != null){
            $this->textHTML .= $text;
        }
        $this->textHTML .= "</a>";
    }

    public function addBR($nbBR = null){
        if ($nbBR != null){
            $i = 1;
            while ($i <= $nbBR - 1){
                $this->textHTML .= "<br />";
                $i++;
            }
        }
        $this->textHTML .= "<br />";
    }

    public function __tostring(){
        return $this->textHTML . '</form>';
    }
//</editor-fold>
}

?>